package tyk.Servlet;

import tyk.Service.UserService;
import tyk.pojo.User;
import tyk.utils.SHA256;

import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 *@title $NAME
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/4/29 16:36
 **/
public class UserServlet extends HttpServlet {
    public void Login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, NoSuchAlgorithmException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String code=request.getParameter("code");
        String codeTest= (String) request.getSession().getAttribute("KAPTCHA_SESSION_KEY");
        request.getSession().removeAttribute("KAPTCHA_SESSION_KEY");

        if(!codeTest.equals(code)){
            request.setAttribute("status","验证码不正确");
            request.getRequestDispatcher("html/Login.jsp").forward(request,response);
            return;
        }
        password=SHA256.hash(password,username.getBytes());
        password=SHA256.hash(password,username.getBytes());
        UserService userService=new UserService(username,password,"null");
        List<User> list = new ArrayList<>();
        int rows=0;
        list=userService.Login();
        rows=list.size();
        if(rows>0) {
            System.out.println("登录成功");
            request.getSession().setAttribute("User", list.get(0));
            try {
                response.sendRedirect("./index.jsp");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else {
            System.out.println("登录失败");
            try {
                request.setAttribute("status","账号或密码错误");
                request.getRequestDispatcher("html/Login.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void Logout(HttpServletRequest request,HttpServletResponse response){
        request.getSession().removeAttribute("User");
        try {
            response.sendRedirect("./index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void UserRegistration(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, NoSuchAlgorithmException {
        String username=request.getParameter("username");
        String password1=request.getParameter("password1");
        String password2=request.getParameter("password2");

        String code=request.getParameter("code");
        String codeTest= (String) request.getSession().getAttribute("KAPTCHA_SESSION_KEY");
        request.getSession().removeAttribute("KAPTCHA_SESSION_KEY");

        if(!codeTest.equals(code)){
            request.setAttribute("status","验证码不正确");
            request.getRequestDispatcher("html/UserRegistration.jsp").forward(request,response);
            return;
        }
        UserService userService=new UserService(username,password1,null);
        if(!password1.equals(password2))
        {
            request.setAttribute("status","两次输入的密码不一致，注册失败");
            try {
                request.getRequestDispatcher("html/UserRegistration.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        userService.setPassword(SHA256.hash(userService.getPassword(),username.getBytes()));
        userService.setPassword(SHA256.hash(userService.getPassword(),username.getBytes()));  //迭代加密
        int rows=userService.Register();
        if(rows>0) {
            System.out.println("注册成功");
            request.setAttribute("status","注册成功，请登录");
            try {
                request.getRequestDispatcher("html/Login.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else {
            System.out.println("注册失败");
            request.setAttribute("status","用户名已存在，注册失败");
            try {
                request.getRequestDispatcher("html/UserRegistration.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void UsernameIsAvailable(HttpServletRequest request,HttpServletResponse response) throws IOException, SQLException {
        response.setContentType("text/plain;charset=UTF-8");
        String username=request.getParameter("username");
        UserService userService=new UserService();
        String sql="select * from user where username = ?;";
        int rows=userService.executeQuery(sql,username);
        if(rows==0) response.getWriter().write("用户名可以使用");
        else response.getWriter().write("用户名已被使用");

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String hidden=request.getParameter("hidden");
        if(hidden==null) hidden=request.getPart("action").toString();
        try {
            Method method = this.getClass().getDeclaredMethod(hidden,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
